use([
    "public/javascripts/service/index_oldService.js",
    "public/javascripts/index_old/index.js",
    "public/javascripts/widget/prompt/prompt.js"
], function(IndexService, indexModel, PromptComponent) {
    var PageComponent = React.createClass({
        //mixins: [React.addons.LinkedStateMixin],
        imOpenStatus:false,    //客服IM打开状态
        componentDidMount: function() {
            var _this = this;
            indexModel.init();

            var userType = cookie.get('userType');
            var primary = cookie.get('primary') == 'true' ? true : false;
            _this.setState({
              'userType':userType,
              'primary':primary
            });
        },
        receiveMsg: function(data) {

        },
        getInitialState: function () {
            var _this = this;
            _this.getUserInfo();
            return this.props ? this.props : {};
        },
        render:function() {
            var _this = this;
            _this.userType = _this.state.userType;
            var data = this.state.data;
            return ~init;
        },
        getUserInfo:function(){    //获取用户基本信息
            var _this = this;
            IndexService.getUserInfo(function (res) {
                if(res.errorCode === 0){
                    _this.setState({loginStatus: true});
                }else{
                    _this.setState({loginStatus: false});
                }
            });
        },
        leaveMsg:function(){    //非登录状态留言
            var _this = this;
            if(!_this.state.leaveMsg || !_this.state.leaveMsg.replace(/(^\s*)|(\s*$)/g, "")){
                $('#leaveMsg').focus();  return;
            }
            var config = {
                urlParams:{
                    'anonyTel':_this.state.leaveMobile,
                    'anonymity':_this.state.leaveName,
                    'content':_this.state.leaveMsg
                }
            };
            IndexService.leaveMsg(config, function (res) {
                if(res.errorCode === 0) {
                    indexModel.hideIM();
                    PromptComponent.init({'type':'success','msg':'留言提交成功！'}).show();
                    _this.setState({leaveMobile: ''});
                    _this.setState({leaveName: ''});
                    _this.setState({leaveMsg: ''});
                }else{
                    PromptComponent.init({'type':'success','msg':'留言提交失败，请您点击提交重试！'}).show();
                }
            });
        },
        // createIm: function(){
        //     var _this = this;
        //     if(!_this.state.loginStatus){    //非登录状态
        //         indexModel.showIM();
        //     }else{     //已登录状态
        //         if(_this.imOpenStatus){    //Im已打开
        //             return;
        //         }
        //         IndexService.createIm(function(res){    //创建会话
        //             if(res.errorCode === 0){
        //                 _this.state.imId = res.data.id;
        //
        //                 indexModel.initIm(res.data);
        //             }
        //             _this.setState({sendMsgText: ''});
        //             _this.imOpenStatus = true;
        //             indexModel.showIM();
        //         });
        //     }
        // },
        // sendMsg: function(){
        //     var _this = this;
        //     if(!_this.state.sendMsgText || !_this.state.imId){
        //         return;
        //     }
        //     var config = {
        //         urlParams:{
        //             'refAskBoardId':_this.state.imId,
        //             'messageBody':_this.state.sendMsgText
        //         }
        //     };
        //     IndexService.sendMsg(config, function (res) {
        //         if(res.errorCode === 0){
        //             indexModel.addImMsg(res.data, _this.state.sendMsgText);
        //             _this.setState({'sendMsgText':''});
        //         }else{
        //             PromptComponent.init({'type':'success','msg':res.msg}).show();
        //         }
        //     });
        // },
        createIm: function(){
            var _this = this;
            if(!_this.state.loginStatus){    //非登录状态
                $("#leaveMsg").val("");
                indexModel.showIM();
            }else{     //已登录状态
                if(_this.imOpenStatus){    //Im已打开
                    return;
                }
                var date={};
                date.createTime = new Date().format('yyyy-MM-dd hh:mm:ss');
                indexModel.initIm(date);
                _this.setState({sendMsgText: ''});
                _this.imOpenStatus = true;
                indexModel.showIM();
            }
        },
        sendMsg: function(){
            var _this = this;
            if(!_this.state.sendMsgText ){
                return;
            }

            if(!_this.state.imId) { // 第一次发消息的时候创建会话
                IndexService.createIm(function (res) {
                    if (res.errorCode === 0) {
                        _this.state.imId = res.data.id;
                        var config = {
                            urlParams: {
                                'refAskBoardId': _this.state.imId,
                                'messageBody': _this.state.sendMsgText
                            }
                        };
                        IndexService.sendMsg(config, function (res) {
                            if (res.errorCode === 0) {
                                indexModel.addImMsg(res.data, _this.state.sendMsgText);
                                _this.setState({'sendMsgText': ''});
                            } else {
                                PromptComponent.init({'type': 'success', 'msg': res.msg}).show();
                            }
                        });
                    }
                })
            }else{
                var config = {
                    urlParams: {
                        'refAskBoardId': _this.state.imId,
                        'messageBody': _this.state.sendMsgText
                    }
                };
                IndexService.sendMsg(config, function (res) {
                    if (res.errorCode === 0) {
                        indexModel.addImMsg(res.data, _this.state.sendMsgText);
                        _this.setState({'sendMsgText': ''});
                    } else {
                        PromptComponent.init({'type': 'success', 'msg': res.msg}).show();
                    }
                });

            }
        },
        closeIm: function(){
            var _this = this;
            _this.setState({imId: ''});
            if(!_this.state.loginStatus){    //非登录状态
                indexModel.hideIM();
            }else{    //登录状态
                _this.imOpenStatus = false;
                indexModel.hideIM();
            }
        },
        changeValue: function(event) {
            var _this = this;
            var obj = {},
                name = event.target.name,
                value = event.target.value;

            obj[name] = value;
            _this.setState(obj);
        },
        isShowImMsg: function(){
            var _this = this,
                styleObj = {};
            if(_this.state.loginStatus){
                styleObj = {display: 'block'};
            }else{
                styleObj = {display: 'none'};
            }
            return styleObj;
        },
        isShowLeave: function(){
            var _this = this,
                styleObj = {};
            if(!_this.state.loginStatus){
                styleObj = {display: 'block'};
            }else{
                styleObj = {display: 'none'};
            }
            return styleObj;
        }
    });
    if(typeof ReactDOM != "undefined") {
        ReactDOM.render(<PageComponent data={storage}/>, document.getElementById('container'));
    }
    module.exports = PageComponent;
});
